package dk.fuddi;

import dk.fuddi.conversion.DeEncodeFactory;
import dk.fuddi.conversion.DeEncodeInterface;
import dk.fuddi.exception.EnDecodeException;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * User: jen
 * Date: May 17, 2007
 * Time: 3:50:06 PM
 */
public class ConversionPanel extends JPanel implements ActionListener, KeyListener, MouseListener {

  public static final String ENCODE = "Encode/Escape";
  public static final String DECODE = "Decode/Unescape";

  private ButtonGroup codeRadioGroup = new ButtonGroup();
  private JComboBox conversionBox = new JComboBox();

  private JTextArea input = new JTextArea();
  private JTextArea output = new JTextArea();

  private JScrollPane inputScrollPane = new JScrollPane(input);
  private JScrollPane outputScrollPane = new JScrollPane(output);

  private JButton doItButton = new JButton("Do it (Ctrl + Enter)");
//  private JButton copyOutputButton = new JButton("Copy output");
//  private JLabel messageLabel = new JLabel(" ");

  public ConversionPanel(Dimension d) {
    super();

    setSize(d);
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    input.addKeyListener(this);
    input.addMouseListener(this);
    doItButton.addActionListener(this);
//    copyOutputButton.addActionListener(new CopyActionListener());

    Dimension maxDimension = new Dimension(1000, 25);

    JPanel conversionJPanel = new JPanel();
    for (DeEncodeInterface deEncodeInterface : DeEncodeFactory.getInstance().getConversions()) {
      conversionBox.addItem(deEncodeInterface);
    }
    conversionJPanel.setSize(maxDimension);
    conversionJPanel.setMaximumSize(maxDimension);
    conversionJPanel.add(conversionBox);
    add(conversionJPanel);

    JPanel codeJPanel = new JPanel();
    codeJPanel.setSize(maxDimension);
    codeJPanel.setMaximumSize(maxDimension);
    initCodeButtons(codeJPanel);
    add(codeJPanel);

    input.setBackground(new Color(211, 211, 211));

    output.setBackground(new Color(190, 190, 190));
    output.setEditable(false);

    Dimension dimension = new Dimension(1000, 250);
    inputScrollPane.setPreferredSize(dimension);
    outputScrollPane.setPreferredSize(dimension);

//    messageLabel.setMinimumSize(new Dimension(100, 20));

    JPanel displayPanel = new JPanel();
    displayPanel.setLayout(new BoxLayout(displayPanel, BoxLayout.Y_AXIS));
    addJComponet(inputScrollPane, displayPanel, "Input");
    addJComponet(doItButton, displayPanel, null);
    addJComponet(outputScrollPane, displayPanel, "Output");
//    addJComponet(copyOutputButton, displayPanel, null);
//    addJComponet(messageLabel, displayPanel, null);

    displayPanel.setSize((int) EnDecodeMain.dimension.getWidth(), (int) EnDecodeMain.dimension.getHeight() - 50);
    add(displayPanel);
  }

  public void addJComponet(JComponent component, Container container, String title) {
    if (StringUtils.isNotBlank(title)) {
      component.setBorder(BorderFactory.createTitledBorder(title));
    }
    component.setAlignmentX(Component.CENTER_ALIGNMENT);
    container.add(component);
  }

  private void initCodeButtons(JPanel codeJPanel) {
    JRadioButton encodeRadio = new JRadioButton(ENCODE);
    encodeRadio.setActionCommand(ENCODE);
    encodeRadio.setSelected(true);
    codeJPanel.add(encodeRadio);

    JRadioButton decodeRadio = new JRadioButton(DECODE);
    decodeRadio.setActionCommand(DECODE);
    codeJPanel.add(decodeRadio);

    codeRadioGroup.add(encodeRadio);
    codeRadioGroup.add(decodeRadio);
  }

  private void deEncode() {
    String text = input.getText().trim();
    if (StringUtils.isNotBlank(text)) {
      String result;
      DeEncodeInterface deEncodeInterface = (DeEncodeInterface) conversionBox.getSelectedItem();
      try {
        if (codeRadioGroup.getSelection().getActionCommand().equals(ENCODE)) {
          result = deEncodeInterface.encode(text);
        } else {
          result = deEncodeInterface.decode(text);
        }
        output.setText(result);
        revalidate();
      } catch (EnDecodeException e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
      }
    }
  }

  public void actionPerformed(ActionEvent e) {
    deEncode();
  }

  public void keyPressed(KeyEvent e) {
    if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
      deEncode();
    }
  }

  public void keyTyped(KeyEvent e) {
  }

  public void keyReleased(KeyEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

/*  private class CopyActionListener implements ActionListener, ClipboardOwner {
    public void actionPerformed(ActionEvent e) {
      if (output.getText() != null && !output.getText().isEmpty()) {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(output.getText()), this);
        messageLabel.setText("Copied text with a length [" + output.getText().length()
            + "] to your clipboard");
      } else {
        messageLabel.setText("No output to copy");
      }
    }

    public void lostOwnership(Clipboard clipboard, Transferable contents) {
      //do nothing
    }
  }*/
}
